//
//  YTUtilityManager.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/2/14.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "YTUtilityManager.h"
#import "NSString+YTExtension.h"
#import <CocoaSecurity/CocoaSecurity.h>
#import <MJRefresh/MJRefresh.h>

NSString * const YTUserLoginSuccessNotification = @"com.yitu8.loginSuccess";
NSString * const YTUserNeedToLoginNotification = @"com.yitu8.needLogin";
NSString * const YTUserRegisterSuccessNotification = @"com.yitu8.regsterSuccess";
NSString * const YTUserUnreadMessageNotification = @"com.yitu8.unreadMessage";
NSString * const YTPickOrderSuccessNofitication = @"com.yitu8.pickOrderSuccess";

@implementation YTUtilityManager

+ (instancetype)manager {
    static YTUtilityManager * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    
    return manager;
}

#pragma mark - 数据获取

- (NSString *)getUserID {
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    return [userdefault objectForKey:kUserIdDefaults];
}

- (NSString *)getUserToken {
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    return [userdefault objectForKey:kUserToken];
}

- (NSString *)getDeviceId {
    NSString * str = [[UIDevice currentDevice].identifierForVendor UUIDString];
    return str;
}

- (BOOL)getCityCacheState {
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSString * str =  [userDefault objectForKey:kCityCache];
    if ([str isEqualToString:@"YES"]) {
        return YES;
    }else{
        return NO;
    }
}

- (BOOL)getCountryCodeCacheState {
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSString * str =  [userDefault objectForKey:kCountryCodeCache];
    if ([str isEqualToString:@"YES"]) {
        return YES;
    }else{
        return NO;
    }
}

- (BOOL)getFlightInfoInland {
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSString * str =  [userDefault objectForKey:kInlandFlightCache];
    if ([str isEqualToString:@"YES"]) {
        return YES;
    }else{
        return NO;
    }
}

- (BOOL)getFlightInfoForeign {
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSString * str =  [userDefault objectForKey:kForeignFlightCache];
    if ([str isEqualToString:@"YES"]) {
        return YES;
    }else{
        return NO;
    }
}

#pragma mark - 用户登入登出操作

- (void)userExitAction {
    
}

- (void)exitUser {
    
}

// 是否需要显示引导页

- (BOOL)isShowGuideView {
    NSString *key = @"CFBundleShortVersionString";
    // 取出沙盒中存储的上次使用软件的版本号
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lastVersion = [defaults stringForKey:key];
    
    // 获得当前软件的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    if ([currentVersion isEqualToString:lastVersion]) {
        return NO;
    } else {
        [defaults setObject:currentVersion forKey:key];
        [defaults synchronize];
        return YES;
    }
}

#pragma mark - 时间判断

-(BOOL)compareLocalDate:(NSString*)localDate withOrderDate:(NSString*)orderDate {
    BOOL ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt22 = [[NSDate alloc] init];
    
    
    dt1 = [df dateFromString:localDate];
    dt22 = [df dateFromString:orderDate];
    
    NSTimeZone * zone = [NSTimeZone localTimeZone];
    NSInteger timeOff = [zone secondsFromGMT];
    NSDate * dt2 = [dt22 dateByAddingTimeInterval:timeOff];
    
    
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //orderDate比localDate大
        case NSOrderedAscending: ci=NO; break;
            //orderDate比localDate小
        case NSOrderedDescending: ci=YES; break;
            //orderDate=localDate
        case NSOrderedSame: ci=YES; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return ci;
}

@end
